package com.example.teaching_server.repository;

import com.example.teaching_server.entity.CheckInRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

/**
 * 打卡记录数据访问层
 */
@Repository
public interface CheckInRecordRepository extends JpaRepository<CheckInRecord, Long> {

    /**
     * 根据活动ID获取所有打卡日期（去重）
     */
    @Query("SELECT DISTINCT r.checkDate FROM CheckInRecord r WHERE r.activity.id = :activityId ORDER BY r.checkDate DESC")
    List<LocalDate> findDistinctDatesByActivityId(@Param("activityId") Long activityId);

    /**
     * 根据活动ID获取包含图片的打卡记录
     */
    @Query("SELECT DISTINCT r FROM CheckInRecord r " +
            "LEFT JOIN FETCH r.images " +
            "WHERE r.activity.id = :activityId " +
            "ORDER BY r.checkDate DESC, r.checkTime DESC")
    List<CheckInRecord> findRecordsWithImagesByActivityId(@Param("activityId") Long activityId);
}
